package com.meilai.project.mapper.business.building;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.controller.mini.dto.MiniSearchBuildingParams;
import com.meilai.project.controller.mini.vo.MiniBuildingDetailVO;
import com.meilai.project.controller.mini.vo.MiniBuildingVO;
import com.meilai.project.dto.business.building.BuildingAreaDTO;
import com.meilai.project.dto.business.building.BuildingAvaAllMPCountVO;
import com.meilai.project.dto.business.building.BuildingOccupyMPCountVO;
import com.meilai.project.dto.business.installOrder.WorkOrderBuidingDTO;
import com.meilai.project.entity.business.building.BuildingArea;
import com.meilai.project.util.entity.PointRange;
import com.meilai.project.vo.IdNameVO;
import com.meilai.project.vo.business.building.BuildingAreaWebBaseVO;
import com.meilai.project.vo.business.building.BuildingAreaWebMapVO;
import com.meilai.project.vo.business.building.BuildingAreaWebVO;
import com.meilai.project.vo.business.installOrder.WorkOrderBuildingVO;
import com.meilai.project.vo.other.SearchAreaVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Mapper
public interface BuildingAreaMapper extends BaseMapper<BuildingArea> {
    MiniBuildingDetailVO getBADetailVOForMini(@Param("id") Long id);

    List<MiniBuildingVO> listByConditionForMini(@Param("search") MiniSearchBuildingParams searchDTO);

    List<BuildingArea> listByCondition(
            @Param("pcaList") List<String> pcaList,
            @Param("pointRanges") List<PointRange> pointRangeList
    );

    List<BuildingAreaWebBaseVO> listAll(String ad_code);

    List<IdNameVO> listAllSimple(String ad_code);

    List<BuildingAreaWebVO> selectPageList(Page<BuildingAreaWebVO> page, @Param("buildingArea") BuildingAreaDTO buildingArea);

    List<BuildingAreaWebMapVO> selectIdNameBySWNE(@Param("sw_lat") BigDecimal sw_lat,
                                                  @Param("ne_lat") BigDecimal ne_lat,
                                                  @Param("sw_lng") BigDecimal sw_lng,
                                                  @Param("ne_lng") BigDecimal ne_lng);

    List<BuildingAvaAllMPCountVO> selectBuildingAvaAllMPCount(@Param("start_at") LocalDate start_at, @Param("end_at") LocalDate end_at);

    List<BuildingOccupyMPCountVO> selectBuildingOccupyMPCount(@Param("start_at") LocalDate start_at, @Param("end_at") LocalDate end_at);

    @Select("select DISTINCT CONCAT(province,city,area)as area,adcode from building_area where ISNULL(deleted_at)")
    List<SearchAreaVO> selectSearchAreaVO();

    List<BuildingArea> listBySaleCondition(@Param("pcaList") List<String> pcaList, @Param("pointRanges") List<PointRange> pointRanges, @Param("buildingTypeIdList") List<Long> buildingTypeIdList);

    List<WorkOrderBuildingVO> listPageBuildingForNewInstallWorkOrder(@Param("searchDTO") WorkOrderBuidingDTO workOrderBuidingDTO);

    List<WorkOrderBuildingVO> listPageBuildingForCheckWorkOrder(@Param("searchDTO") WorkOrderBuidingDTO workOrderBuidingDTO);

    @Select("select * from `building_area` ba where ba.`id` in(\n" +
            "select mp.`building_area_id` from `media_point` mp where mp.`id` in (\n" +
            "select p.`media_point_id` from `media_point_choose_daterange_mediapoint` p " +
            "left join `bill_sale` b on b.`choose_id`=p.`choose_id` and p.`daterange_id`=b.`daterange_id` where b.`id`=#{bill_sale_id})) " +
            "and ISNULL(ba.`deleted_at`) and ba.`status`=1")
    List<BuildingArea> listBuildingOfBillSale(@Param("bill_sale_id") Long bill_sale_id);

    List<BuildingArea> listBuildingOnWorkerId(Page<BuildingArea> page, @Param("worker_id") Long worker_id, @Param("build_name") String build_name);

    BuildingAreaWebVO one(@Param("id") Long id);

    List<JSONObject> getBuildingAreaBySmartBox(@Param("building_ids") List<Long> building_ids);
}
